#include "drv_dji.h"


/**
 * @brief:    初始化 DJI 发送通道
 */
dji_channel_t dji_channel_1;
dji_channel_t dji_channel_2;
dji_channel_t dji_channel_3;
void DJI_Channel_Init(void)
{
  // channel 1: 0x1ff
  dji_channel_1.channel_id = DJI_CHANNEL_1FF;
  for (uint8_t i = 0; i < 4; i++) {
    dji_channel_1.data[i] = 0;
  }

  // channel 2: 0x200
  dji_channel_2.channel_id = DJI_CHANNEL_200;
  for (uint8_t i = 0; i < 4; i++) {
    dji_channel_2.data[i] = 0;
  }

  // channel 3: 0x2ff
  dji_channel_2.channel_id = DJI_CHANNEL_2FF;
  for (uint8_t i = 0; i < 4; i++) {
    dji_channel_3.data[i] = 0;
  }
}

/**
 * @brief:    将电机数据注册到通道
 */
void DJI_RegData(uint8_t channel_id, uint8_t in_id, uint16_t data)
{
  if (in_id <= 0 || in_id >= 9) {
    return;
  }
  if (in_id > 4) {
    in_id -= 4;
  }
  
  switch (channel_id)
  {
    case 1: {
      dji_channel_1.data[in_id - 1] = data;
      break;
    }
    case 2: {
      dji_channel_2.data[in_id - 1] = data;
      break;
    }
    case 3: {
      dji_channel_3.data[in_id - 1] = data;
      break;
    }
    default: {
      break;
    }
  }
}

/**
 * @brief:    通道数据包的发送
 */
uint8_t DJI_SendData(dji_channel_t* dji_ptr)
{
  uint8_t data[8];
  data[0] = (uint8_t)((dji_ptr->data[0] >> 8) & 0xff); 
  data[1] = (uint8_t)(dji_ptr->data[0] & 0xff);
  data[2] = (uint8_t)((dji_ptr->data[1] >> 8) & 0xff); 
  data[3] = (uint8_t)(dji_ptr->data[1] & 0xff);
  data[4] = (uint8_t)((dji_ptr->data[2] >> 8) & 0xff); 
  data[5] = (uint8_t)(dji_ptr->data[2] & 0xff);
  data[6] = (uint8_t)((dji_ptr->data[3] >> 8) & 0xff); 
  data[7] = (uint8_t)(dji_ptr->data[3] & 0xff);
  return canx_send_data(&hcan1, dji_ptr->channel_id, data, 8);
}
